package com.hhd.pdf.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfDiv;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

public class PdfUtils {

    private static BaseColor color = new BaseColor(89, 89, 89);

    public static BaseFont createBaseFont(String fontName) throws DocumentException, IOException {
        if (fontName == null) {
            fontName = "simsun.ttc";
        }
        String fontPre = fontName.substring(fontName.lastIndexOf(".") + 1);
        if (fontPre.equals("ttc")) {
            // ttc格式的字体需要加上后缀
            fontName = fontName + ",0";
        }
        String font = "/font/" + fontName;
        return BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }

    public static PdfPTable createTable(float[] columnsWidth) {
        PdfPTable table = new PdfPTable(columnsWidth);
        table.setWidthPercentage(100);
        return table;
    }

    public static PdfPCell getPdfPCell() {
        PdfPCell cell1 = new PdfPCell();
        cell1.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
        cell1.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell1.setRowspan(1);
        cell1.setColspan(1);
        cell1.setBorder(0);
        return cell1;
    }

    public static PdfPCell getPdfPCell(Phrase phrase) {
        PdfPCell cell1 = new PdfPCell(phrase);
        cell1.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
        cell1.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell1.setRowspan(1);
        cell1.setColspan(1);
        cell1.setBorder(0);
        return cell1;
    }

    public static PdfPCell createCell(Image image) {
        PdfPCell cell = new PdfPCell(image);
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell.setRowspan(1);
        cell.setColspan(1);
        cell.setBorder(0);
        return cell;
    }

    public static PdfPCell createCell() {
        PdfPCell cell = new PdfPCell();
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell.setRowspan(1);
        cell.setColspan(1);
        cell.setBorder(0);
        return cell;
    }


    public static Font createFont(int fontSize, BaseColor color, int style) throws DocumentException, IOException {
//        BaseFont bfChinese = PdfUtils.createBaseFont("msyh.ttc");
        BaseFont bfChinese = PdfUtils.createBaseFont("pingfang.ttf");
        Font font = new Font(bfChinese, fontSize, style);
        font.setColor(color);
        return font;
    }

    public static Font createFont(int fontSize, BaseColor color) throws DocumentException, IOException {
//        BaseFont bfChinese = PdfUtils.createBaseFont("msyh.ttc");
        BaseFont bfChinese = PdfUtils.createBaseFont("pingfang.ttf");
        Font font = new Font(bfChinese, fontSize, Font.NORMAL);
        font.setColor(color);
        return font;
    }


    public static Paragraph createParagraph(String text, Font font, int align) {
        Paragraph p = new Paragraph(new Phrase(text, font));
        p.setAlignment(align);
        p.setFont(font);
        return p;
    }

    public static Paragraph createParagraph() {
        return new Paragraph();
    }

    public static Paragraph createParagraph(String text) throws DocumentException, IOException {
        Font font = createFont(12, color);
        Paragraph p = new Paragraph(new Phrase(text, font));
        p.setAlignment(Paragraph.ALIGN_LEFT);
        return p;
    }

    public static PdfDiv createDiv(int paddingTop, int paddingBottom) {
        PdfDiv div = new PdfDiv();
        div.setPaddingTop(paddingTop);
        div.setPaddingBottom(paddingBottom);
        return div;
    }

    public static PdfDiv createDiv(Element p, int paddingTop, int paddingBottom) {
        PdfDiv div = new PdfDiv();
        div.addElement(p);
        div.setPaddingTop(paddingTop);
        div.setPaddingBottom(paddingBottom);
        return div;
    }

    public static Paragraph createParagraph(Font font, int align) {
        Paragraph p = new Paragraph();
        p.setAlignment(align);
        p.setFont(font);
        return p;
    }

    public static BaseColor createColor(int a, int b, int c) {
        return new BaseColor(a, b, c);
    }

    public static Image getAutograph(String name) throws BadElementException, IOException {
        return Image.getInstance(Objects.requireNonNull(PdfUtils.class.getResource("/autograph/" + name)));
    }

    public static Image getImage(String name) throws BadElementException, IOException {
        return Image.getInstance(Objects.requireNonNull(PdfUtils.class.getResource("/images/" + name)));
    }

    public static boolean isPhrase(Element element1) {
        if (element1 instanceof Phrase) {
            return true;
        }
        return false;
    }

    public static PdfPTable createTable(int colNum, int pageWidth) {
        float[] floats = new float[colNum];
        Arrays.fill(floats, pageWidth * 1.0F / colNum);
        return createTable(floats);
    }

    public static Phrase toPhrase(Element element) {
        return (Phrase) element;
    }

    public static PdfDiv wrapDiv(Element table, int paddingLeft) {
        PdfDiv pdfDiv = new PdfDiv();
        pdfDiv.setPaddingLeft(paddingLeft);
        pdfDiv.addElement(table);
        return pdfDiv;
    }

    public static PdfDiv wrapDiv(Element table, int paddingTop, int paddingRight, int paddingBottom, int paddingLeft) {
        PdfDiv pdfDiv = new PdfDiv();
        pdfDiv.setPaddingTop(paddingTop);
        pdfDiv.setPaddingRight(paddingRight);
        pdfDiv.setPaddingBottom(paddingBottom);
        pdfDiv.setPaddingLeft(paddingLeft);
        pdfDiv.addElement(table);
        return pdfDiv;
    }

    public static Element createDivider(int paddingTop, int paddingRight, int paddingBottom, int paddingLeft) {
        PdfPTable table = createTable(new float[]{50});
        PdfPCell cell = createCell();
        cell.setBorderWidthBottom(1);
        cell.setBorderColor(new BaseColor(233, 233, 233));
        table.addCell(cell);
        return wrapDiv(table, paddingTop, paddingRight, paddingBottom, paddingLeft);
    }

    public static Phrase changeFont(Phrase phrase, Font font) {
        return new Phrase(phrase.getContent(), font);
    }

    public static Image getAutographByPath(String formulaStringValue) throws BadElementException, IOException {
        return Image.getInstance(formulaStringValue);
    }
}
